package com.doyens.controller;

import com.doyens.client.UserClient;
import com.doyens.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/consumer/user")
//@DefaultProperties(defaultFallback = "fallbackMethod")
public class UserController {

//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private UserClient client;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping
    @ResponseBody
    //@HystrixCommand
    public User querUserById(@RequestParam("id" )Long id){
//        List<ServiceInstance> instances = discoveryClient.getInstances("service-provider");
//        ServiceInstance instance = instances.get(0);
//        return this.restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/user/"+id,User.class);
//        return this.restTemplate.getForObject("http://service-provider/user/"+id,String.class);
//        if (id ==1){
//            throw  new RuntimeException();
//        }
//        try {
//            Thread.sleep(6000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return client.queryUserById(id);
    }

    public String fallbackMethod (){

        return "服务器繁忙，请稍后再试";
    }
}
